#include "poll.h"

#include <windows.h>
#include <io.h>
#include <iostream>

int poll(pollfd *fds, nfds_t nfds, int timeout) {
	if (fds->events != POLLIN)	//I have no idea what to do with anything else
		return 0;

	int stdHandle = fds->fd;
	HANDLE pipeHandle = (HANDLE)_get_osfhandle(stdHandle);
	if (pipeHandle == (HANDLE)-1)
		return 0;
	
	DWORD bytesInPipe = 0;
	BOOL res = PeekNamedPipe(pipeHandle, NULL, 0, NULL, &bytesInPipe, NULL);
	if (res == FALSE)
		return 0;
	
	fds->revents = 0;
	if (bytesInPipe)
		fds->revents = POLLIN;

	return 1;
}
